package pers.kui.mall.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pers.kui.mall.common.ApiRestResponse;
import pers.kui.mall.common.Constant;
import pers.kui.mall.exception.ExceptionEnum;
import pers.kui.mall.model.pojo.Category;
import pers.kui.mall.model.pojo.User;
import pers.kui.mall.model.request.AddCategoryReq;
import pers.kui.mall.model.request.UpdateCategoryReq;
import pers.kui.mall.service.CategoryService;
import pers.kui.mall.service.UserService;
import pers.kui.mall.vo.CategoryVO;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * @author: ldk
 * 描述: 目录Controller
 */
@Controller
public class CategoryController {
    @Autowired
    UserService userService;

    @Autowired
    CategoryService categoryService;

    /**
     * 后台添加目录
     * @param session
     * @param addCategoryReq 添加的分类目录信息
     * @return
     */
    @ApiOperation("后台添加目录")
    @PostMapping("admin/category/add")
    @ResponseBody
    public ApiRestResponse addCategory(HttpSession session, @Valid @RequestBody AddCategoryReq addCategoryReq) {
        //1、判断商品分类目录名称，级别父id，时序是否为空,必须按照顺序进行书写，不然会报错
        // if (addCategoryReq.getName() == null || addCategoryReq.getType() == null || addCategoryReq.getParentId() == null || addCategoryReq.getOrderNum() == null) {
        //     return ApiRestResponse.error(ExceptionEnum.PARA_NOT_NULL);
        // }
        //2.获取session中的用户
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if(currentUser == null){
            return ApiRestResponse.error(ExceptionEnum.NEED_LOGIN);
        }
        //3、校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        //是管理员，进行分类添加
        if (adminRole){
            categoryService.add(addCategoryReq);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(ExceptionEnum.NEED_ADMIN);
        }
    }

    @ApiOperation("更新后台目录")
    @PostMapping("admin/category/update")
    @ResponseBody
    public ApiRestResponse updateCategory(@Valid @RequestBody UpdateCategoryReq updateCategoryReq,HttpSession session){
        User currentUser = (User) session.getAttribute(Constant.IMOOC_MALL_USER);
        if(currentUser == null){
            return ApiRestResponse.error(ExceptionEnum.NEED_LOGIN);
        }
        //3、校验是否是管理员
        boolean adminRole = userService.checkAdminRole(currentUser);
        //是管理员，进行分类添加
        if (adminRole){
            Category category = new Category();
            BeanUtils.copyProperties(updateCategoryReq ,category);
            categoryService.update(category);
            return ApiRestResponse.success();
        }else{
            return ApiRestResponse.error(ExceptionEnum.NEED_ADMIN);
        }
    }
    @ApiOperation("删除后台目录")
    @PostMapping("admin/category/delete")
    @ResponseBody
    public ApiRestResponse deleteCategory(@RequestParam Integer id){
        categoryService.delete(id);
        return ApiRestResponse.success();
    }

    @ApiOperation("后台目录列表")
    @GetMapping("admin/category/list")
    @ResponseBody
    public ApiRestResponse listCategoryForAdmin(@RequestParam Integer pageNum , @RequestParam Integer pageSize){
        PageInfo pageInfo = categoryService.listForAdmin(pageNum, pageSize);
        return ApiRestResponse.success(pageInfo);
    }

    @ApiOperation("前台目录列表")
    @GetMapping("/category/list")

    @ResponseBody
    public ApiRestResponse listCategoryForCustom(){
        //CategoryVO类 要进行序列化
        List<CategoryVO> categoryVOS = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVOS);

    }
}
